package daoLivraria;

import java.sql.*;
import java.util.*;

public class GeneroDAO {

    private Connection con;
    private Statement stmt;

    public GeneroDAO() {
        try {
            this.con = DataSourceDAO.getConnection();
            // criar statement
            this.stmt = con.createStatement();
        } catch (Exception sql1) {
            System.out.println("Erro em base de dados: " + sql1.toString());
        }
    }

    public void finalize() {
        //try {
        //    this.con.close();
        //} catch (SQLException sql1) {}
    }

    public GeneroTO find(int id) {
        try {
            ResultSet rs = this.stmt.executeQuery("select * from generos where genero_id = "+id);
            if (rs.next()) {
                GeneroTO genero = new GeneroTO();
                genero.setGenero_id(rs.getInt("genero_id"));
                genero.setDescricao(rs.getString("descricao"));
                return genero;
            } else {
                return null;
            }
        } catch (SQLException sql1) {
            System.out.println("ERRO: "+sql1);
            return null;
        }
    }

    public ArrayList<GeneroTO> listAll() {
        try {
            ArrayList<GeneroTO> lista = new ArrayList();
            ResultSet rs = this.stmt.executeQuery("select * from generos order by genero_id");
            while (rs.next()) {
                GeneroTO genero = new GeneroTO();
                genero.setGenero_id(rs.getInt("genero_id"));
                genero.setDescricao(rs.getString("descricao"));
                lista.add(genero);
            }
            return lista;
        } catch (SQLException sql1) {
            System.out.println(sql1.toString());
            return null;
        }
    }




}
